package com.only4play.portal.cp.message.jsonmessage;

import com.only4play.portal.cp.bean.message.AbstractCpMessage;
import com.only4play.portal.cp.bean.message.jsonmessage.CpJsonMessage;
import com.only4play.portal.cp.bean.message.jsonmessage.CpJsonOutMessage;
import com.only4play.portal.cp.message.CpMessageHandler;
import com.only4play.portal.cp.message.CpMessageRouter;
import com.only4play.portal.cp.message.CpMessageRouterRule;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.regex.Pattern;

public class JsonCpMessageRouterRule extends CpMessageRouterRule<CpJsonOutMessage, CpJsonMessage> {


    protected JsonCpMessageRouterRule(CpMessageRouter<CpJsonOutMessage, CpJsonMessage> routerBuilder) {
        super(routerBuilder);
    }

    @Override
    protected boolean attemptMatch(CpJsonMessage cpJsonMessage) {
            return
                (this.getFromUser() == null || this.getFromUser().equals(cpJsonMessage.getFromUserName()))
                    && (this.agentId == null || this.agentId.equals(Integer.valueOf(cpJsonMessage.getAgentId())))
                    && (this.msgType == null || this.msgType.equalsIgnoreCase(cpJsonMessage.getMsgType()))
                    && (this.event == null || this.event.equalsIgnoreCase(cpJsonMessage.getEvent()))
                    && (this.eventKey == null || this.eventKey.equalsIgnoreCase(cpJsonMessage.getEventKey()))
                    && (this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(cpJsonMessage.getEventKey())))
                    && (this.content == null || this.content.equals(StringUtils.trimToNull(cpJsonMessage.getContent())))
                    && (this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(cpJsonMessage.getContent())))
                    && (this.matcher == null || this.matcher.match(cpJsonMessage));
    }
}
